package com.example.controller;

import com.example.common.Page;
import com.example.common.Result;
import com.example.pojo.Book;
import com.example.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/* restful风格api */
//@Controller
@RestController // 等同于ResponseBody + Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    // /book post方式
//    @RequestMapping(method = RequestMethod.POST)
//    @ResponseBody
    @PostMapping// 等同于RequestMapping(method = RequestMethod.POST)
    public Object addBook(@RequestBody Book book) {
        System.out.println("新增书籍");
        return bookService.addBook(book);
    }

    // /book put请求
//    @RequestMapping(method = RequestMethod.PUT)
//    @ResponseBody
    @PutMapping
    public Object updateBook(@RequestBody Book book) {
        System.out.println("修改书籍");
        return bookService.updateBook(book);
    }

    // /book/1 delete请求
    @DeleteMapping("/{id}")
    public Object deleteBookById(@PathVariable int id) {
        System.out.println("删除一个书籍：" + id);
        return bookService.deleteBookById(id);
    }
    // /book/1 get请求
    @GetMapping("/{id}")
    public Object getBookById(@PathVariable int id) {
        System.out.println("查询单个书籍：" + id);
        return bookService.getBookById(id);
    }

    // /book/all get请求
    @GetMapping("/all")
    public Object selectAll() {
        System.out.println("查询全部");
        return null;
    }

    /**
     * 输入4个条件（可填）和2个页码信息（必填的）
     * 输出总数据条数、总页数和表格所需要的集合数据
     * @return
     */
    @PostMapping("/queryPage")
    public Object selectByPage(@RequestBody Page<Book> page) {
        System.out.println("条件分页查询");
        System.out.println(page);
        return Result.success(bookService.selectByPage(page));
    }
}
